#include "test_pwm.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_common.h"



void cm_test_pwm_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("OPEN",cmd[2],strlen("OPEN")))
    {
        // uint32_t pwm_frequency,pwm_duty;
        // uint8_t dev = CM_PWM_DEV_NUM;
        // if(0 == strcmp(cmd[3],"PWM0"))
        // {
        //     dev = CM_PWM_DEV_0;
        //     cm_iomux_set_pin_func(OPENCPU_TEST_PWM0_IOMUX);
        // }
        // else if(0 == strcmp(cmd[3],"PWM1"))
        // {
        //     dev = CM_PWM_DEV_1;
        //     cm_iomux_set_pin_func(OPENCPU_TEST_PWM1_IOMUX);
        // }
        // else if(0 == strcmp(cmd[3],"PWM2"))
        // {
        //     dev = CM_PWM_DEV_2;
        //     cm_iomux_set_pin_func(OPENCPU_TEST_PWM2_IOMUX);
        // }
        // else{
        //     cm_demo_printf("param error!\n");
        //     return;
        // }
        
        // cm_demo_printf("pwm test start... ...\n");

        // //使用PWM0测试
        // pwm_frequency = atoi((char *)cmd[4]);
        // pwm_duty = atoi((char *)cmd[5]);
        
        // cm_demo_printf("pwm%d pwm_frequency=%d,pwm_duty =%d\n", dev,pwm_frequency,pwm_duty);
        
        // cm_demo_printf("cm_pwm_open ret:%d\n", cm_pwm_open(dev, pwm_frequency,pwm_duty));
    }
    else if(0 == strncmp("START",cmd[2],strlen("START")))
    {
        uint32_t period, period_h;
        uint8_t dev = CM_PWM_DEV_NUM;

#if CM_OPENCPU_MODEL_ML307A
        if(0 == strcmp(cmd[3],"PWM0"))
        {
            dev = CM_PWM_DEV_0;
            cm_iomux_set_pin_func(OPENCPU_TEST_PWM0_IOMUX);
        }
        else if(0 == strcmp(cmd[3],"PWM1"))
        {
            dev = CM_PWM_DEV_1;
            cm_iomux_set_pin_func(OPENCPU_TEST_PWM1_IOMUX);
        }
        else if(0 == strcmp(cmd[3],"PWM2"))
        {
            dev = CM_PWM_DEV_2;
            cm_iomux_set_pin_func(OPENCPU_TEST_PWM2_IOMUX);
        }
        else{
            cm_demo_printf("param error!\n");
            return;
        }
#endif
#if CM_OPENCPU_MODEL_ML305A
        if(0 == strcmp(cmd[3],"PWM0"))
        {
            dev = CM_PWM_DEV_0;
            cm_iomux_set_pin_func(CM_IOMUX_PIN_69,CM_IOMUX_FUNC_FUNCTION1);
        }
        else if(0 == strcmp(cmd[3],"PWM1"))
        {
            dev = CM_PWM_DEV_1;
            cm_iomux_set_pin_func(CM_IOMUX_PIN_70,CM_IOMUX_FUNC_FUNCTION1);
        }
        else if(0 == strcmp(cmd[3],"PWM2"))
        {
            dev = CM_PWM_DEV_2;
            cm_iomux_set_pin_func(CM_IOMUX_PIN_56,CM_IOMUX_FUNC_FUNCTION3);
        }
        else if(0 == strcmp(cmd[3],"PWM3"))
        {
            dev = CM_PWM_DEV_3;
            cm_iomux_set_pin_func(CM_IOMUX_PIN_57,CM_IOMUX_FUNC_FUNCTION3);
        }
        else{
            cm_demo_printf("param error!\n");
            return;
        }
#endif
        
        cm_demo_printf("pwm test start... ...\n");

        //使用PWM0测试
        period = atoi((char *)cmd[4]);
        period_h = atoi((char *)cmd[5]);
        
        cm_demo_printf("pwm%d period=%d,period_h =%d\n", dev,period,period_h);
        int32_t pwm_open_ns = cm_pwm_open_ns(dev, period,period_h);
        cm_demo_printf("cm_pwm_start ret:%d\n",pwm_open_ns);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,pwm_open_ns,cm_pwm_open_ns);
    }
    else if(0 == strncmp("STOP",cmd[2],strlen("STOP")))
    {
        uint8_t dev = CM_PWM_DEV_NUM;
        if(0 == strcmp(cmd[3],"PWM0"))
        {
            dev = CM_PWM_DEV_0;
        }
        else if(0 == strcmp(cmd[3],"PWM1"))
        {
            dev = CM_PWM_DEV_1;
        }
        else if(0 == strcmp(cmd[3],"PWM2"))
        {
            dev = CM_PWM_DEV_2;
        }
        else if(0 == strcmp(cmd[3],"PWM3"))
        {
            dev = CM_PWM_DEV_3;
        }
        else{
            cm_demo_printf("param error!\n");
            return;
        }
        int32_t pwm_close = cm_pwm_close(dev);
        cm_demo_printf("cm_pwm_open ret:%d\n",pwm_close);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,pwm_close,cm_pwm_close);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}